# -*- coding: utf-8 -*-

#	This file is part of Kanji-Notification.

#	Kanji-Notification is free software: you can redistribute it 
#	and/or modify it under the terms of the GNU General Public License 
#	as published by the Free Software Foundation, either version 3 of 
#	the License, or	(at your option) any later version.
#
#	Kanji-Notification is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with Kanji-Notification. If not, see <http://www.gnu.org/licenses/>.



import string
from xml.etree.ElementTree import parse
from kanjidata import *

def parse_xml(file_name):
	root = Root()
	r = parse(file_name).getroot()
	fontChild = r.find("fontName")
	if (fontChild is not None):
		root.fontName = fontChild.text
	sleepChild = r.find("sleep")
	if (sleepChild is not None):
		root.sleep = int(sleepChild.text)
	else:
		#60 seconds is the default value
		root.sleep = 60
	for child in r.findall("group"):
		root.groups[child.attrib['id']] = Group(child.attrib['id'])
	for child in r.findall("kanji"):
		kanji = Kanji()
		kanji.uchar = child.find("uchar").text.strip()
		kanji.kun = [ aux.text.strip() for aux in child.findall("kun")] 
		kanji.on = [ aux.text.strip() for aux in child.findall("on")] 
		kanji.examples = [ Example(aux.find("japanese").text.strip(), aux.find("hiragana").text.strip(), aux.find("english").text.strip()) for aux in child.findall("example")] 
		if 'groups' in child.attrib:
			for group in string.split(child.attrib['groups']):
				root.groups[group].add_kanji(kanji)		
		root.add_kanji(kanji)
	return root
	
if __name__ == "__main__":
	root = parse_xml("kanji.xml")
	for group in root.groups.values():
		print group
	for kanji in root.kanji:
		print kanji
		for k in kanji.kun:
			print k
		for k in kanji.on:
			print k	
		for ex in kanji.examples:
			print ex	
		print
